<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = show_btn = true ">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-popconfirm title="确定批量删除?" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu" v-if="hasPermission()">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="name"
          label="班级名称"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="time"
          label="创建时间"
          width="300">
      </el-table-column>
      <el-table-column
          prop="count"
          label="学生人数"
          width="150">
      </el-table-column>

      <el-table-column
          prop="icon"
          label="操作"
          width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="editHandle(scope.row.id)">修改</el-button>
          <span>&nbsp;&nbsp;</span>
          <template>
            <el-popconfirm title="确定删除?" @confirm="delHandle(scope.row.id)">
              <el-button type="danger" size="mini" slot="reference" v-if="hasPermission()">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页器-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 15, 30, 60]"
        :page-size="size"
        :total="total">
    </el-pagination>

    <!--    编辑框-->
    <el-dialog
        title="编辑框"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">
        <el-form-item label="班级名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(editForm)" v-if="show_btn">立即新增</el-button>
          <el-button type="primary" @click="submitEdit(editForm)" v-else>立即修改</el-button>
          <el-button @click="resetForm(editForm)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div style="text-align: center; padding-bottom: 10px;">&COPY;copyleft@海饼干</div>
  </div>
</template>

<script>
export default {
  name: "ClassList",
  data() {
    return {
      show_btn: true,
      delBtlStatu: true,
      total: 0,
      size: 10,
      current: 1,
      dialogVisible: false,
      editForm: {},
      tableData: [],
      editFormRules: {
        name: [
          {required: true, message: '请输入班级名称', trigger: 'blur'}
        ],
      },
      multipleSelection: [],
    }
  },
  created() {
    this.getClassList()
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      console.log("勾选")
      console.log(val)
      this.multipleSelection = val;
      this.delBtlStatu = val.length === 0
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getClassList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getClassList()
    },
    resetForm(formName) {
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm(this.editForm)
    },
    getClassList() {
      this.$http.get(this.$settings.BASE_URL + '/user/student_class/', {
        params: {
          page: this.current,
          size: this.size
        },
      }).then(res => {
        if (res.data.code === 20000) {
          this.tableData = res.data.result.results
          this.total = res.data.result.count
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    submitForm(formName) {
      if (!formName.name) {
        this.$message({
          type: 'warning',
          message: '输入不能为空',
          duration: 3000,
        })
      } else {
        this.$http({
          url: this.$settings.BASE_URL + '/user/student_class/',
          method: 'post',
          data: this.editForm,
        }).then(res => {
          if (res.data.code === 20000) {
            this.dialogVisible = false
            this.resetForm(formName)
            this.getClassList()
            this.$message({
              duration: 3000,
              message: '创建成功',
              type: 'success',
            });
          } else {
            this.$message({
              type: 'error',
              message: '访问出错或班级名重复',
              duration: 3000,
            })

          }
        })
      }
    },
    submitEdit(formName) {
      if (!formName.name) {
        this.$message({
          type: 'warning',
          message: '输入不能为空',
          duration: 3000,
        })
      } else {
        this.$http({
          url: this.$settings.BASE_URL + '/user/edit_class/update_class/',
          method: 'post',
          data: this.editForm,
        }).then(res => {
          if (res.data.code === 20000) {
            this.dialogVisible = false
            this.show_btn = true
            this.resetForm(formName)
            this.getClassList()
            this.$message({
              duration: 3000,
              message: '修改成功',
              type: 'success',
            });
          } else {
            this.$message({
              type: 'warning',
              message: res.data.msg,
              duration: 3000,
            })
          }
        })
      }
    },
    editHandle(id) {
      this.show_btn = false
      this.$http({
        url: this.$settings.BASE_URL + `/user/student_class/${id}/`,
        method: 'get',
      }).then(res => {
        if (res.data.code === 20000) {
          this.editForm = res.data.result
          this.dialogVisible = true
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    delHandle(id) {
      let ids = []
      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      this.$http({
        url: this.$settings.BASE_URL + '/user/edit_class/del_class/',
        method: 'post',
        data: ids,
      }).then(res => {
        if (res.data.code === 20000) {
          this.dialogVisible = false
          this.getClassList()
          this.$message({
            duration: 3000,
            message: '删除成功',
            type: 'success',
          });
        } else {
          this.$message({
            type: 'error',
            message: '访问出错',
            duration: 3000,
          })
        }
      })
    },
  }
}
</script>

<style scoped>

</style>
